package elicecn.stock;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class Pinyin {

	public static final String BLANK = "";
	public static final int MAXLENGTH = 960;
	private static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
	static {
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	}

	/**
	 * 转换拼音（多音字取第一个音，非汉字原样输出）。可供查询用
	 * 
	 * @param str
	 * @return
	 */
	public static String toPinyinSimple(String str) {
		if (str == null || str == "")
			return BLANK;
		StringBuffer buf = new StringBuffer();
		str = str.toLowerCase();
		for (int i = 0; i < str.length(); i++) {
			char word = str.charAt(i);
			String[] pinyinArray = null;
			try {
				pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word,
						format);
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
			if (pinyinArray != null) {
				buf.append(pinyinArray[0]);
			} else if (word >= 'a' && word <= 'z') {
				buf.append(word);
			}
		}
		return buf.toString().toUpperCase();
	}

	/**
	 * 转换拼音首字母（多音字取第一个音，非汉字原样输出）。可供查询用
	 * 
	 * @param str
	 * @return
	 */
	public static String toPinyinHeaderSimple(String str) {
		if (str == null || str == "")
			return BLANK;
		StringBuffer buf = new StringBuffer();
		str = str.toLowerCase();
		for (int i = 0; i < str.length(); i++) {
			char word = str.charAt(i);
			String[] pinyinArray = null;
			try {
				pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word,
						format);
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
			if (pinyinArray != null) {
				buf.append(pinyinArray[0].charAt(0));
			} else if (word >= 'a' && word <= 'z') {
				buf.append(word);
			}
		}
		return buf.toString().toUpperCase();
	}

	/**
	 * 汉字转换成拼音简拼+全拼
	 * 
	 * @param str
	 * @return
	 */
	public static String toPinyinHeaderAndFull(String str) {
		return toPinyinHeader(str) + "/" + toPinyin(str);
	}

	/**
	 * 汉字转换成拼音简拼+全拼，并限制最大字节数
	 * 
	 * @param str
	 * @param maxlength
	 * @return
	 */
	public static String toPinyinHeaderAndFull(String str, int maxlength) {
		String pinyin = toPinyinHeader(str) + "/" + toPinyin(str);
		if (pinyin.length() > maxlength) {
			pinyin = pinyin.substring(0, maxlength - 100);
		}
		return pinyin;
	}

	/**
	 * 转换拼音（多音组合以半角/分隔）。可供数据库用
	 * 
	 * @param str
	 * @return
	 */
	public static String toPinyin(String str) {
		return toPinyinHeader(str, false);
	}

	public static String toPinyinHeader(String str) {
		return toPinyinHeader(str, true);
	}

	/**
	 * 转换拼音首字母（多音组合以半角/分隔）。可供数据库用
	 * 
	 * @param str
	 * @param flag
	 *            true首字母 false全拼
	 * @return
	 */
	public static String toPinyinHeader(String str, boolean flag) {
		if (str == null || str.length() == 0)
			return BLANK;
		List<String[]> list = new ArrayList<String[]>();
		str = str.toLowerCase();
		for (int i = 0; i < str.length(); i++) {
			char word = str.charAt(i);
			String[] pinyinArray = null;
			try {
				pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word,
						format);
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
			if (pinyinArray != null) {
				list.add(pinyinArray);
			} else if (word < 0xFF) {
				String[] words = { String.valueOf(word) };
				list.add(words);
			}
		}
		List<String> result = mergeWords(flag, null, list, 0);
		Set<String> rst = new HashSet<String>(result);
		StringBuffer buf = new StringBuffer();

		int size = rst.size();
		String[] strArr = new String[size];
		rst.toArray(strArr);
		for (int i = 0; i < strArr.length; i++) {
			buf.append(strArr[i]);
			if (i + 1 < size) {
				buf.append("/");
			}
		}
		return buf.toString().toUpperCase();
	}

	/**
	 * 合并拼音
	 * 
	 * @param flag
	 *            ture = 只合并首字母
	 * @param list
	 * @param list2
	 * @param idx
	 * @return
	 */
	public static List<String> mergeWords(boolean flag, List<String> list,
			List<String[]> list2, int idx) {
		if (idx == list2.size()) {
			return list;
		}
		List<String> result = new ArrayList<String>();
		int size = list != null ? list.size() : 0;
		if (size == 0) {
			String[] strArr = list2.get(idx);
			for (int i = 0; i < strArr.length; i++) {
				result.add(flag ? strArr[i].substring(0, 1) : strArr[i]);
			}
			return mergeWords(flag, result, list2, idx + 1);
		} else {
			for (int i = 0; i < size; i++) {
				String[] strArr = list2.get(idx);
				for (int j = 0; j < strArr.length; j++) {
					String str = flag ? strArr[j].substring(0, 1) : strArr[j];
					String letters = list.get(i) + str;
					result.add(letters);
				}
			}
			return mergeWords(flag, result, list2, idx + 1);
		}
	}

	public static void main(String[] args) {
		// System.out.println(toPinyinSimple("中国银行"));
		System.out.println("A".getBytes().length);
		System.out.println("中".getBytes().length);
		System.out.println("國".getBytes().length);
	}
}
